<template>
  <div>
    <Row>
      <i-col span="12" push="1">
        <Form :model="resetPassword" :label-width="120" ref="resetPassword" :rules="resetPasswordRule">
          <Form-item label="用户名">
            {{currentUser.loginName}}
          </Form-item>
          <Form-item label="旧密码" prop="oldPwd">
            <Input type="password" autocomplete="off" v-model="resetPassword.oldPwd" placeholder="请输入原密码"></Input>
          </Form-item>

          <Form-item label="新密码" prop="newPwd1">
            <Input type="password" v-model="resetPassword.newPwd1" placeholder="请输入新密码"></Input>
          </Form-item>

          <Form-item label="确认密码" prop="newPwd2">
            <Input type="password" v-model="resetPassword.newPwd2" placeholder="请再次输入新密码"></Input>
          </Form-item>

          <Form-item>
            <Button type="primary" @click="submitReset">提交</Button>
            <Button type="ghost" style="margin-left: 8px" @click="resetPassword.oldPwd='',resetPassword.newPwd1='',resetPassword.newPwd2=''">取消</Button>
          </Form-item>
        </Form>
      </i-col>
    </Row>
  </div>
</template>
<script>
  import {mapGetters} from 'vuex'
  import {loginByUsername, updatePwd} from '@api/common/user'

  export default {
    name: 'trackingCode',
    data () {
      let resetPassword = {
        oldPwd: '',
        newPwd1: '',
        newPwd2: ''
      }
      let resetPasswordRule = {
        oldPwd: [
          {required: true, message: '密码不能为空！', trigger: 'blur'},
          {type: 'string', message: '密码不能小于6位！', min: 6, trigger: 'blur'}
        ],
        newPwd1: [
          {required: true, message: '密码不能为空！', trigger: 'blur'},
          {type: 'string', message: '密码不能小于6位！', min: 6, trigger: 'blur'}
        ],
        newPwd2: [
          {required: true, message: '密码不能为空！', trigger: 'blur'},
          {type: 'string', message: '密码不能小于6位！', min: 6, trigger: 'blur'}
        ]
      }
      let datas = {resetPassword, resetPasswordRule}
      return datas
    },
    methods: {
      submitReset () {
        const username = this.currentUser.loginName
        const password = this.resetPassword.oldPwd
        loginByUsername(username, password).then(res => {
          if (+res.data.isSuccess === 1) {
            if (this.resetPassword.newPwd1 !== this.resetPassword.newPwd2) {
              this.$Message.error('确认密码与新密码不一致!')
              return false
            }
            const newPwd = this.resetPassword.newPwd2
            updatePwd({newPwd}).then(res => {
              if (res.data.isSuccess === 1) {
                this.resetPassword.newPwd1 = ''
                this.resetPassword.newPwd2 = ''
                this.resetPassword.oldPwd = ''
                this.$Message.success('修改成功!')
                this.$store.dispatch('logout').then(() => {
                  this.$router.push({path: '/user/login_board'})
                }).catch(err => {
                  this.$message.error(err)
                })
              }
            })
          } else {
            this.$Message.error('原密码错误!')
            return false
          }
        })
      }
    },
    computed: {
      ...mapGetters(['currentUser'])
    },
    mounted () {
    }
  }
</script>
<style>
</style>
